Control Flow and Decison making Practice Problems in C++

Posted on December 18, 2023 by Vishesh Namdev
Python C C++ Java
C++ Programming

Basic Practice Problems based on chapter which you learn in previous Tutorials.

1. Even and Odd

#include <iostream>Copy Code
int main() {
// Declare variables to store the two numbers
double num1, num2;

// Input the two numbers
std::cout << "Enter the first number: ";
std::cin >> num1;

std::cout << "Enter the second number: ";
std::cin >> num2;

// Perform addition
double sum = num1 + num2;

// Display the result
std::cout << "Sum: " << sum << std::endl;

return 0;
}

The program checks whether the number is even or odd using the modulus operator (%). If the remainder when dividing by 2 is zero, the number is even; otherwise, it's odd.

2. Largest among three numbers

#include <iostream>Copy Code
#include <iostream>

int main() {
// Declare three variables to store the numbers
int num1, num2, num3;

// Input the three numbers
std::cout << "Enter three integers: ";
std::cin >> num1 >> num2 >> num3;

// Find the largest among three numbers
if (num1 >= num2 && num1 >= num3) {
std::cout << num1 << " is the largest." << std::endl;
} else if (num2 >= num1 && num2 >= num3) {
std::cout << num2 << " is the largest." << std::endl;
} else {
std::cout << num3 << " is the largest." << std::endl;
}

return 0;
}

3. Generate Multiplication table in C++

#include <iostream>Copy Code
int main() {
// Declare a variable to store the number for which the table will be generated
int number;

// Input the number
std::cout << "Enter a number: ";
std::cin >> number;

// Display the multiplication table for the entered number
std::cout << "Multiplication Table for " << number << ":\n";
for (int i = 1; i <= 10; ++i) {
std::cout << number << " x " << i << " = " << (number * i) << std::endl;
}

return 0;
}

4. Palindrome Numbers

A number is a Palindrome number if the reverse of the number and the numbers itself are equal i.e. if the number and its reverse are the same then a number is a palindrome number.

#include <stdio.h>Copy Code
int main() {
int number, originalNumber, reversedNumber = 0, remainder;

// Input the number
std::cout << "Enter an integer: ";
std::cin >> number;

// Store the original number for comparison later
originalNumber = number;
// Reverse the number
while (number > 0) {
remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
}
// Check if the reversed number is equal to the original number
if (originalNumber == reversedNumber) {
std::cout << originalNumber << " is a palindrome." << std::endl;
} else {
std::cout << originalNumber << " is not a palindrome." << std::endl;
}

return 0;
}